#import <SpringBoard/SBApplication.h>
#import <SpringBoard/SBApplicationController.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
#import <UIKit/UIApplication.h>
#import "../LockInfo/Plugin.h"

#import "PITodoView.h"
#import "PIPlugin.h"


@implementation PITodoView

@synthesize dot, due, priority, name, action, flag;

#define ROW1_HEIGHT 16
#define ROW2_HEIGHT 14
#define ROW_WIDTH 320
#define LEFT_MARGIN 22
#define DOT_HEIGHT 9
#define DOT_WIDTH 9
#define SEPARATOR_WIDTH 4



+ (PITodoView *)createView:(CGRect)frame table:(LITableView *) table
{
	PITodoView* v = [[[PITodoView alloc] initWithFrame:frame] autorelease];
	v.backgroundColor = [UIColor clearColor];
	
	v.dot = [[[PIDotView alloc] initWithFrame:CGRectMake(4, 4, 9, 9)] autorelease];
	v.dot.backgroundColor = [UIColor clearColor];
	
	v.name = [table labelWithFrame:CGRectZero];
	v.name.frame = CGRectMake(22, 0, 275, 16);
	v.name.backgroundColor = [UIColor clearColor];
	
	v.due = [table labelWithFrame:CGRectZero];
	v.due.frame = CGRectMake(75, 16, 200, 14);
	v.due.backgroundColor = [UIColor clearColor];

	v.priority = [table labelWithFrame:CGRectZero];
	v.priority.frame = CGRectMake(22, 16, 30, 14);
	
	v.action = [[[PIActionView alloc] initWithFrame:CGRectMake(300, 0, 16, 16)] autorelease];
	v.action.backgroundColor = [UIColor clearColor];
	
	v.flag = [[[UIImageView alloc] initWithFrame:CGRectMake(282, 0, 16, 16)] autorelease];
	v.flag.backgroundColor = [UIColor clearColor];
	
	[v addSubview:v.dot];	
	[v addSubview:v.name];
	[v addSubview:v.action];
	[v addSubview:v.flag];
	[v addSubview:v.priority];
	[v addSubview:v.due];
	
	return v;
}

- (void)setViewForTable:(LITableView*)table withPlugin:(PIPlugin*)pi title:(NSString*)title due:(NSString*)dates overdue:(BOOL)overdue importance:(int)importance priority:(NSString*)prty action:(int)actn flagged:(BOOL)flagged showIcons:(BOOL)showIcons
{
	self.name.style = [[table.theme.summaryStyle copy] autorelease];
	self.name.text = title;

	UIColor* importanceColor = [pi colorFromImportance:importance];
	if (importanceColor != nil) {
		self.dot.color = importanceColor;
		self.dot.hidden = false;
		[self.dot setNeedsDisplay];
	}
	else {
		self.dot.hidden = true;
	}

	if ((dates != nil) && (prty != nil) && ([prty length] > 0)) {
		self.priority.text = [NSString stringWithFormat:@" %@ ", prty];
		self.priority.style = [[table.theme.detailStyle copy] autorelease];
		self.priority.layer.cornerRadius = 6.0;
		self.priority.layer.masksToBounds= YES;
		self.priority.backgroundColor = importanceColor;
		self.priority.hidden = false;
	}
	else
		self.priority.hidden = true;
		
	if (flagged && showIcons) {
		NSString* imagePath = [NSString stringWithFormat:@"%@/%@.png", [pi appPath], @"star_yellow_small"];
		DebugLog(@"LI:PITodo: Loading flag image %@", imagePath);

		self.flag.image = [UIImage imageWithContentsOfFile:imagePath];
		self.flag.hidden = false;
	}
	else {
		self.flag.hidden = true;
	}
	
	if (dates != nil) {
		if (overdue) {
			self.due.style = [[table.theme.detailStyle copy] autorelease];
			self.due.style.textColor = [pi overdueColor];
		}
		else {
			self.due.style = table.theme.detailStyle;
		}
		
		self.due.text = dates;
		self.due.frame.size.height = 14;
		self.due.hidden = false;
	}
	else {
		self.due.frame.size.height = 0;
		self.due.hidden = true;
	}
	
	NSString* actionIcon = [pi iconPathFromAction:actn];
	if (showIcons && (actionIcon != nil)) {
		self.action.color = [pi colorFromAction:actn];
		self.action.icon = actionIcon;
		[self.action setNeedsDisplay];
		self.action.hidden = false;
	}
	else  {
		self.action.hidden = true;
	}
	
	// Layout the view
	if (!self.due.hidden) {
		int leftMargin = LEFT_MARGIN;
		if (!self.priority.hidden) {
			CGRect rect = self.priority.frame;
			rect.origin.x = leftMargin;
			leftMargin += rect.size.width + SEPARATOR_WIDTH;
			self.priority.frame = rect;
			[self.priority setTextAlignment:UITextAlignmentCenter];
		}

		CGRect rect = self.due.frame;
		rect.origin.x = leftMargin;
		rect.size.width = ROW_WIDTH - rect.origin.x;
		self.due.frame = rect;
	}

	int rightMargin = ROW_WIDTH - SEPARATOR_WIDTH - 1;
	if (!self.action.hidden) {
		CGRect rect = self.action.frame;
		rect.origin.x = rightMargin - rect.size.width;
		rightMargin = rightMargin - rect.size.width - SEPARATOR_WIDTH;
		self.action.frame = rect;
	}

	if (!self.flag.hidden) {
		CGRect rect = self.flag.frame;
		rect.origin.x = rightMargin - rect.size.width;
		rightMargin = rightMargin - rect.size.width - SEPARATOR_WIDTH;
		self.flag.frame = rect;
	}
	
	CGRect rect = self.name.frame;
	rect.origin.x = LEFT_MARGIN;
	rect.size.width = rightMargin - rect.origin.x;
	self.name.frame = rect;
}

@end
